{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import rabbitpy\n",
    "import time\n",
    "from ch6 import utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Open the channel and connection\n",
    "connection = rabbitpy.Connection()\n",
    "channel = connection.channel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the response queue that will automatically delete, is not durable and \n",
    "# is exclusive to this publisher\n",
    "queue_name = 'response-queue-%s' % os.getpid()\n",
    "response_queue = rabbitpy.Queue(channel, \n",
    "                                queue_name,\n",
    "                                auto_delete=True,\n",
    "                                durable=False,\n",
    "                                exclusive=True)\n",
    "# Declare the response queue\n",
    "if response_queue.declare():\n",
    "    print('Response queue declared')\n",
    "\n",
    "# Bind the response queue\n",
    "if response_queue.bind('rpc-replies', queue_name):\n",
    "    print('Response queue bound')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Iterate through the images to send RPC requests for\n",
    "for img_id, filename in enumerate(utils.get_images()):\n",
    "    \n",
    "    print('Sending request for image #%s: %s' % (img_id, filename))\n",
    "\n",
    "    # Create the message\n",
    "    message = rabbitpy.Message(channel,\n",
    "                               utils.read_image(filename),\n",
    "                               {'content_type': utils.mime_type(filename),\n",
    "                                'correlation_id': str(img_id),\n",
    "                                'reply_to': queue_name},\n",
    "                               opinionated=True)\n",
    "\n",
    "    # Pubish the message\n",
    "    message.publish('fanout-rpc-requests')\n",
    "\n",
    "    # Loop until there is a response message\n",
    "    message = None\n",
    "    while not message:\n",
    "        time.sleep(0.5)\n",
    "        message = response_queue.get()\n",
    "\n",
    "    # Ack the response message\n",
    "    message.ack()\n",
    "\n",
    "    # Caculate how long it took from publish to response\n",
    "    duration = (time.time() - \n",
    "                time.mktime(message.properties['headers']['first_publish']))\n",
    "    \n",
    "    print('Facial detection RPC call for image %s total duration: %s' % \n",
    "          (message.properties['correlation_id'], duration))\n",
    "\n",
    "    # Display the image in the IPython notebook interface\n",
    "    utils.display_image(message.body, message.properties['content_type'])\n",
    "\n",
    "print('RPC requests processed')\n",
    "\n",
    "# Close the channel and connection\n",
    "channel.close()\n",
    "connection.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
